package tms.model;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 * Taken without modification (except for method documentation) from the Window Builder tutorial here:
 * http://code.google.com/javadevtools/wbpro/
 * 
 * AbstractModelObject represents an object with property change support
 * 
 * @author lobas_av
 * 
 */
public abstract class AbstractModelObject {
	private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
			this);

	/**
	 * Add a property change listener to the property change support
	 * 
	 * @param listener a property change listener to be added
	 */
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(listener);
	}

	/**
	 * Add a property change listener to the property change support
	 * 
	 * @param propertyName the name of the property whose listener will be added
	 * @param listener the property change listener associated with the property name and to be added 
	 */
	public void addPropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
	}

	/**
	 * Remove a property change listener from the property change support
	 * 
	 * @param listener a property change listener to be removed
	 */
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(listener);
	}

	/**
	 * Remove a property change listener from the property change support
	 * 
	 * @param propertyName the name of the property whose listener will be removed
	 * @param listener the property change listener associated with the property name and to be removed
	 */
	public void removePropertyChangeListener(String propertyName,
			PropertyChangeListener listener) {
		propertyChangeSupport.removePropertyChangeListener(propertyName,
				listener);
	}

	/**
	 * Fire a property update to any registered listeners
	 * No event is fired if the old and new values are equal or null.
	 * 
	 * @param propertyName the name of the property that has changed
	 * @param oldValue the old value of the property
	 * @param newValue the new value of the property
	 */
	protected void firePropertyChange(String propertyName, Object oldValue,
			Object newValue) {
		propertyChangeSupport.firePropertyChange(propertyName, oldValue,
				newValue);
	}
}